<template>
  <div>
    <input style="display: none" type="text" :value="value" />
    <el-select
      allow-create
      v-bind="$attrs"
      style="width: 100%"
      default-first-option
      filterable
      remote
      v-model="selected"
      :remote-method="setOrgName"
      :clearable="clearable"
      :placeholder="placeholder"
      :size="size"
      @change="handleItemChange"
    >
      <el-option
        v-for="p of this.list"
        :key="p.agency_id"
        :label="p.agency_name"
        :value="p.agency_id"
      ></el-option>
    </el-select>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: '',
    },
    size: {
      type: String,
      default: '',
    },
    placeholder: {
      type: String,
      default: '请输入名称',
    },

    orgClass: {
      type: String,
      default: '1',
    },
    clearable: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      selected: null,
      list: {},
    }
  },
  computed: {},
  watch: {
    value(val) {
      this.selected = val
    },
    orgClass() {
      this.init()
    },
  },
  created() {
    this.init()
  },
  mounted() {},
  methods: {
    setOrgName(val) {
      this.init(val)
    },
    init(val) {
      const params = {
        index: 1,
        size: 3,
        address_code: this.$utils.getAddressCode(),
        'agency_type.eq': this.orgClass,
        agency_name: val || '',
      }
      this.$api
        .getData(this.$serviceName, 'credit_agency_info', params, { agency_name: 'desc' })
        .then(res => {
          this.list = res.data.data
          this.selected = this.value
        })
    },
    handleItemChange(val) {
      if (!this.autoChange) {
        this.$emit('input', val)
        const i = this.list.findIndex(it => it.agency_id === val)
        let m = ''
        if (i > -1) {
          m = this.list[i]
        }
        this.$emit('change', m)
      }
    },
    getSelected() {
      return this.selected
    },
  },
}
</script>

<style scoped></style>
